package com.easyctba.core.controller;

import java.util.List;

import com.easyctba.core.domain.CtbaFiasCategory;
import com.easyctba.core.domain.bo.FiasCategoryAddBo;
import com.easyctba.core.domain.vo.FiasCascaderVo;
import com.easyctba.core.domain.vo.FiasCategoryAndClassVo;
import com.easyctba.core.domain.vo.FiasTransferVo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.easyctba.common.idempotent.annotation.RepeatSubmit;
import com.easyctba.common.log.annotation.Log;
import com.easyctba.common.web.core.BaseController;
import com.easyctba.common.mybatis.core.page.PageQuery;
import com.easyctba.common.core.domain.R;
import com.easyctba.common.core.validate.AddGroup;
import com.easyctba.common.core.validate.EditGroup;
import com.easyctba.common.log.enums.BusinessType;
import com.easyctba.common.excel.utils.ExcelUtil;
import com.easyctba.core.domain.vo.CtbaFiasCategoryVo;
import com.easyctba.core.domain.bo.CtbaFiasCategoryBo;
import com.easyctba.core.service.ICtbaFiasCategoryService;
import com.easyctba.common.mybatis.core.page.TableDataInfo;

/**
 * fias大类
 * 前端访问路由地址为:/core/fiasCategory
 *
 * @author Huang
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/fiasCategory")
public class CtbaFiasCategoryController extends BaseController {

    private final ICtbaFiasCategoryService ctbaFiasCategoryService;

    /**
     * 查询fias大类列表
     */
    @SaCheckPermission("core:fiasCategory:list")
    @GetMapping("/list")
    public TableDataInfo<CtbaFiasCategoryVo> list(CtbaFiasCategoryBo bo, PageQuery pageQuery) {
        return ctbaFiasCategoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出fias大类列表
     */
    @SaCheckPermission("core:fiasCategory:export")
    @Log(title = "fias大类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CtbaFiasCategoryBo bo, HttpServletResponse response) {
        List<CtbaFiasCategoryVo> list = ctbaFiasCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "fias大类", CtbaFiasCategoryVo.class, response);
    }

    /**
     * 获取fias大类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("core:fiasCategory:query")
    @GetMapping("/{id}")
    public R<CtbaFiasCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(ctbaFiasCategoryService.queryById(id));
    }

    /**
     * 新增fias大类
     */
    @SaCheckPermission("core:fiasCategory:add")
    @Log(title = "fias大类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CtbaFiasCategoryBo bo) {
        return toAjax(ctbaFiasCategoryService.insertByBo(bo));
    }

    /**
     * 修改fias大类
     */
    @SaCheckPermission("core:fiasCategory:edit")
    @Log(title = "fias大类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CtbaFiasCategoryBo bo) {
        return toAjax(ctbaFiasCategoryService.updateByBo(bo));
    }

    /**
     * 删除fias大类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("core:fiasCategory:remove")
    @Log(title = "fias大类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(ctbaFiasCategoryService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 新增大类
     * @param bo 大类业务对象
     * @return NULL
     */
    @PostMapping("/front")
    public R<Void> addCategory(@Validated(AddGroup.class) @RequestBody FiasCategoryAddBo bo){
        return ctbaFiasCategoryService.addCategory(bo);
    }

    /**
     * 根据fias id简单列表
     * @param id FIAS ID
     * @return List<CtbaFiasCategory>
     */
    @GetMapping("/list/{id}")
    public R<List<CtbaFiasCategory>> list(@NotNull(message = "FIAS分析id不能为空") @PathVariable Long id){
        return ctbaFiasCategoryService.list(id);
    }

    /**
     * 删除大类
     */
    @DeleteMapping("/front/{id}")
    public R<Void> delete(@NotNull(message = "FIAS大类id不能为空") @PathVariable Long id){
        return ctbaFiasCategoryService.delete(id);
    }

    /**
     * 查询分类表
     * @param id FIAS分析id
     * @return 分类表VO
     */
    @GetMapping("/front/detail/{id}")
    public R<List<FiasCategoryAndClassVo>> detail(@NotNull(message = "FIAS分析id不能为空") @PathVariable Long id){
        return ctbaFiasCategoryService.detail(id);
    }

    /**
     * 返回Element级联选择器格式的分类表
     * @param id FIAS分析id
     */
    @GetMapping("/front/cascader/{id}")
    public R<List<FiasCascaderVo>> cascader(@NotNull(message = "FIAS分析id不能为空") @PathVariable Long id){
        return ctbaFiasCategoryService.cascader(id);
    }

    @GetMapping("/front/transfer/{id}")
    public R<List<FiasTransferVo>> transfer(@NotNull(message = "FIAS分析id不能为空") @PathVariable Long id ){
        return ctbaFiasCategoryService.transfer(id);
    }
}
